package com.czl.module_park.fragment

import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.CarAceessItemBean
import com.czl.module_park.BR
import com.czl.module_park.R
import com.czl.module_park.adapter.CarAccessAdapter
import com.czl.module_park.databinding.FragmentCarAccessBinding
import com.czl.module_park.databinding.HeadCarAccessBinding
import com.czl.module_park.viewmodel.CarAccessViewModel

@Route(path = AppConstants.Router.Park.F_CAR_ACCESS)
class CarAccessFragment : BaseFragment<FragmentCarAccessBinding,CarAccessViewModel>() {

    private lateinit var mAdapter: CarAccessAdapter
//    private var inputString=""
//    private var carAccessList: ArrayList<CarAceessItemBean>? = arrayListOf()
    override fun initContentView(): Int {
        return R.layout.fragment_car_access
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        viewModel.tvTitle.set("停车抬杆")
        reload()
        initAdapter()
    }
    private fun initAdapter() {
        mAdapter = CarAccessAdapter(this)
//        val inflate = HeadCarAccessBinding.inflate(layoutInflater)
//        mAdapter.addHeaderView(inflate.root)
        binding.ryCommon.apply {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        viewModel.loadCompleteEvent.observe(this, { data ->
//            if (inputString.isEmpty()){
                handleRecyclerviewData(
                    data == null,
                    data as MutableList<*>?,
                    mAdapter,
                    binding.ryCommon,
                    binding.smartCommon,
                    1,
                    true
                )
//            }else{
////                ^[\u4e00-\u9fa5_a-zA-Z0-9]+$
//                carAccessList?.clear()
//                for (item in data){
//                    if (item.deviceName.matches(Regex.fromLiteral(inputString))){
//                        carAccessList?.add(item)
//                    }
//                }
//                handleRecyclerviewData(
//                    carAccessList == null,
//                    carAccessList,
//                    mAdapter,
//                    binding.ryCommon,
//                    binding.smartCommon,
//                    1,
//                    true
//                )
//            }
        })
//        viewModel.inputChangeEvent.observe(this,{
//            inputString=it
//            var tempCarAcessList: ArrayList<CarAceessItemBean> = arrayListOf()
//            for (item in carAccessList!!){
//                if (item.deviceName.matches(Regex.fromLiteral(inputString))){
//                    tempCarAcessList.add(item)
//                }
//            }
//            mAdapter.setList(tempCarAcessList)
//        })
    }
    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }
}